package com.mike.mybatis;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionManager;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.InputStream;

/**
 * mybatis不同的使用方式
 */
public class App {
    public static void main(String[] args) throws Exception {
        /*
            通过SqlSessionManager获取SqlSession
            1.通过SqlSessionFactoryBuilder读取配置文件创建SqlSessionFactory
            2.通过SqlSessionFactory的openSession()获取sqlSession
         */
//        InputStream is = Resources.getResourceAsStream("mybatis.xml");
//        SqlSessionManager sqlSessionManager = SqlSessionManager.newInstance(is);
//        SqlSession sqlsession2 = sqlSessionManager.openSession();

//        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
//        MysqlDao mysqlDao = (MysqlDao) applicationContext.getBean("mysqlDao");
//        SqlSession sqlsession = mysqlDao.getSqlSession();

//        ContentBanner contentBanner0 = sqlsession2.selectOne("com.mike.mybatis.ContentBannerMapper.selectBanner", "cqll");
//        ContentBanner contentBanner1 = sqlsession2.selectOne("com.mike.mybatis.ContentBannerMapper.selectBannerNew", "tlpx");
//        ContentBanner contentBanner3 = new DBBuilder().one(sqlsession2);

//        ContentBanner contentBanner = sqlsession.getMapper(ContentBannerMapper.class).selectBanner("cqll");
//
//        ContentBanner contentBanner2 = sqlsession.selectOne("com.mike.mybatis.ContentBannerMapper.selectBanner", "cqll");

//        System.out.println(contentBanner0);
//        System.out.println(contentBanner1);
//        System.out.println(contentBanner3);
//        System.out.println(contentBanner);
//        System.out.println(contentBanner2);

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        ContentBanner contentBanner =  DBBuilder.build(ContentBanner.class).one();
        System.out.println(contentBanner);
    }
}
